package com.rpc.rpcservercontainer;

import com.google.common.collect.ImmutableMap;
import com.rpc.rpcserverdef.RpcServer;
import java.util.Map;
import java.util.ServiceLoader;


class RpcServers {
    private final static Map<String, Class<? extends RpcServer>> map;
    
    static 
    {
        ImmutableMap.Builder<String, Class<? extends RpcServer>> b = ImmutableMap.builder();
        
        for (RpcServer rs : ServiceLoader.load(RpcServer.class)) 
        {
            Class<? extends RpcServer> clazz = rs.getClass();
            b.put(rs.getName().toUpperCase(), clazz);
        }
        
        map = b.build();
    }
    
    static RpcServer getRpcServerByName(String name) 
    {
        Class<? extends RpcServer> clazz = map.get(name.toUpperCase());
        
        if (clazz == null)
        {
            return null;
        }
        
        try {
            return (RpcServer) clazz.newInstance();
        } catch (Exception ex) {
            return null;
        } 
        
    }
}
